﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Utilities.QuickSql;
using MySql.Data;

namespace Deviseur_Cs2i_Car
{
    public partial class AddType : Form
    {
        private TYPE Type;
        public AddType()
        {
            InitializeComponent();
            this.Text = "Ajouter un type de voiture";
            this.buttonSubmitType.Text = "Ajouter";
        }
        public AddType(int NumType)
        {
            InitializeComponent();
            this.Text = "Modifier un type de voiture";
            this.buttonSubmitType.Text = "Modifier";

            Table TypeVoiture = Program.Db.QueryTable("SELECT LibelleType, DescriptionType FROM TYPE WHERE NumType = " + NumType);
            foreach (Record r in TypeVoiture)
            {
                this.textLibelle.Text = r.Values[0].ToString();
                this.richTextDescr.Text = r.Values[1].ToString();
                Type = new TYPE(NumType, this.textLibelle.Text.ToString(), this.richTextDescr.Text.ToString());
            }
        }

        private void buttonSubmitType_Click(object sender, EventArgs e)
        {
            if (textLibelle.Text.ToString() != "" && richTextDescr.Text.ToString() != "")
            {
                if (this.buttonSubmitType.Text == "Ajouter")
                {
                    Type = new TYPE(this.textLibelle.Text.ToString(), this.richTextDescr.Text.ToString());

                    Thread addThread = new Thread(new ThreadStart(addType_Thread));
                    addThread.Start();
                }
                else if (this.buttonSubmitType.Text == "Modifier")
                {
                    Type.set_LibelleType(this.textLibelle.Text.ToString());
                    Type.set_DescriptionType(this.richTextDescr.Text.ToString());
                    Thread updateThread = new Thread(new ThreadStart(updateType_Thread));
                    updateThread.Start();
                }
            }
            else
            {
                MessageBox.Show("Veuillez remplir tous les champs", "Erreur de saisie");
            }
        }

        private void addType_Thread()
        {
            Invoke(new MethodInvoker(Disabled));
            Type.new_Type();
            Invoke(new MethodInvoker(CloseFen));
        }
        private void updateType_Thread()
        {
            Invoke(new MethodInvoker(Disabled));
            Type.modify_Type();
            Invoke(new MethodInvoker(CloseFen));
        }

        private void Disabled()
        {
            this.textLibelle.Enabled = false;
            this.richTextDescr.Enabled = false;
            this.buttonSubmitType.Enabled = false;
        }
        private void CloseFen()
        {
            this.Close();
        }
    }
}
